/*
 *  Copyright 2009 joma7188.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.google.code.pj2r.resources.mocks.request;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 *
 * Mock request that keeps request header info.
 */
public class MockHeadersHttpServletRequest extends MockPathHttpServletRequest {

	private HashMap headers = new HashMap();

	public MockHeadersHttpServletRequest(String pathInfo, String method) {
		super(pathInfo, method);
	}

	public void putHeader(String header, String value) {
		List values = (List) headers.get(header);
		if (values == null) {
			values = new ArrayList();
		}
		values.add(value);
		headers.put(header, values);
	}

	public String getHeader(String name) {
		List values = (List) headers.get(name);
		String value = ((values == null) || (values.isEmpty())) ? null : (String) values.get(0);
		return value;
	}

	public Enumeration getHeaderNames() {
		final Iterator  keyIterator = headers.keySet().iterator();
		return new Enumeration() {

			public boolean hasMoreElements() {
				return keyIterator.hasNext();
			}

			public Object nextElement() {
				return keyIterator.next();
			}
		};
	}

	public Enumeration getHeaders(String name) {
		List values = (headers.get(name) == null) ? Collections.EMPTY_LIST : (List) headers.get(name);
		final Iterator valueIterator = values.iterator();
		return new Enumeration() {

			public boolean hasMoreElements() {
				return valueIterator.hasNext();
			}

			public Object nextElement() {
				return valueIterator.next();
			}
		};
	}
}
